package com.scorpio.common.utils;


import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MathUtil {
    public static final String NUMERIC = "1234567890";
    public static final String LETTER = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
    public static final int MONEY_TWO_MULTIPLE = 100;

    /**
     * 保留小数
     *
     * @param f     浮点数据
     * @param digit 保留位数，小于1 将默认等于1
     * @return
     */
    public static String fromDecima(float f, int digit) {
        if (digit < 1) digit = 1;
        StringBuilder sb = new StringBuilder();
        sb.append("#0.");
        for (int i = 0; i < digit; i++) {
            sb.append("0");
        }
        DecimalFormat df = new DecimalFormat(sb.toString());
        return df.format(f);
    }

    public static double fromDecimaToDouble(double f, int digit) {
        return Double.valueOf(fromDecima((float) f, digit));
    }

    public static float fromDecimaToFloat(float f, int digit) {
        return Float.valueOf(fromDecima(f, digit));
    }

    /**
     * 获取随机指定范围字符串
     *
     * @param strType 抽取字符
     * @param initStr 随机数前缀 不需要可为空
     * @param length  随机数长度
     * @return
     */
    public static String getRandLetter(String strType, String initStr, int length) {
        initStr = initStr == null ? "" : initStr;
        StringBuilder txSecret = new StringBuilder();
        txSecret.append(initStr);
        for (int i = 0; i < length; i++) {
            int r = (int) (Math.random() * strType.length());
            txSecret.append(strType.substring(r, r + 1));
        }
        return txSecret.toString();
    }

    /**
     * 获取随机数
     *
     * @param initStr 前置字符
     * @param length  随机数长度
     * @return
     */
    public static String getSum(String initStr, int length) {
        String sum = "";
        for (int i = 0; i < length; i++) {
            sum += "" + (int) (Math.random() * 10);
        }
        return (initStr == null ? "" : initStr) + sum;
    }

    //获取随机字符（包含数字和字母）
    public static String getRandLetter(String initStr, int length) {
        return getRandLetter(LETTER, initStr, length);
    }

    //获取随机字符（包含数字和字母）
    public static String getRandLetter(int length) {
        return getRandLetter(LETTER, "", length);
    }

    //是否數字 包括负数
    public static boolean isNumeric(String str) {
        if (str == null) return false;
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches() || str.trim().equals("")) {
            return false;
        }
        return true;
    }

    //是否數字 包括负数
    public static boolean isNumerics(String... str) {
        for (int i = 0; i < str.length; i++) {
            if (!isNumeric(str[i])) return false;
        }
        return true;
    }

    public static float toFloatValue(Float value) {
        if (value == null) return 0;
        return value;
    }

    public static double toDoubleValue(Double value) {
        if (value == null) return 0;
        return value;
    }

    public static long toLongValue(Long value) {
        if (value == null) return 0;
        return value;
    }

    public static int toIntegerValue(Integer value) {
        if (value == null) return 0;
        return value;
    }
}
